package it.unibo.refolding.pico.ui;

import it.unibo.refolding.common.MyRuntimeException;

import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;

import javax.swing.JComponent;
import javax.swing.JInternalFrame;

public class UiUtil {

  public static void addKeyShortcutForClosing(final JInternalFrame internalFrame, JComponent panel) {
    panel.setFocusable(true);
    panel.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W
          && (e.isControlDown() | (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0)) {
          try {
            internalFrame.setClosed(true);
          } catch (PropertyVetoException pve) {
            throw new MyRuntimeException("Could not close internal window.", pve);
          }
        }
      }
    });
  }
}
